/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package multi.agent.simulation.dao;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.*;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import multi.agent.simulation.dao.exceptions.IllegalOrphanException;
import multi.agent.simulation.dao.exceptions.NonexistentEntityException;
import multi.agent.simulation.entity.Nodo;
import multi.agent.simulation.entity.Rutas;

/**
 *
 * @author Owner
 */
public class NodoJpaController implements Serializable {

    public NodoJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Nodo nodo) {
        if (nodo.getRutasCollection() == null) {
            nodo.setRutasCollection(new ArrayList<Rutas>());
        }
        if (nodo.getRutasCollection1() == null) {
            nodo.setRutasCollection1(new ArrayList<Rutas>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Rutas> attachedRutasCollection = new ArrayList<Rutas>();
            for (Rutas rutasCollectionRutasToAttach : nodo.getRutasCollection()) {
                rutasCollectionRutasToAttach = em.getReference(rutasCollectionRutasToAttach.getClass(), rutasCollectionRutasToAttach.getRutasPK());
                attachedRutasCollection.add(rutasCollectionRutasToAttach);
            }
            nodo.setRutasCollection(attachedRutasCollection);
            Collection<Rutas> attachedRutasCollection1 = new ArrayList<Rutas>();
            for (Rutas rutasCollection1RutasToAttach : nodo.getRutasCollection1()) {
                rutasCollection1RutasToAttach = em.getReference(rutasCollection1RutasToAttach.getClass(), rutasCollection1RutasToAttach.getRutasPK());
                attachedRutasCollection1.add(rutasCollection1RutasToAttach);
            }
            nodo.setRutasCollection1(attachedRutasCollection1);
            em.persist(nodo);
            for (Rutas rutasCollectionRutas : nodo.getRutasCollection()) {
                Nodo oldNodoOfRutasCollectionRutas = rutasCollectionRutas.getHasta();
                rutasCollectionRutas.setHasta(nodo);
                rutasCollectionRutas = em.merge(rutasCollectionRutas);
                if (oldNodoOfRutasCollectionRutas != null) {
                    oldNodoOfRutasCollectionRutas.getRutasCollection().remove(rutasCollectionRutas);
                    oldNodoOfRutasCollectionRutas = em.merge(oldNodoOfRutasCollectionRutas);
                }
            }
            for (Rutas rutasCollection1Rutas : nodo.getRutasCollection1()) {
                Nodo oldNodo1OfRutasCollection1Rutas = rutasCollection1Rutas.getDesde();
                rutasCollection1Rutas.setDesde(nodo);
                rutasCollection1Rutas = em.merge(rutasCollection1Rutas);
                if (oldNodo1OfRutasCollection1Rutas != null) {
                    oldNodo1OfRutasCollection1Rutas.getRutasCollection1().remove(rutasCollection1Rutas);
                    oldNodo1OfRutasCollection1Rutas = em.merge(oldNodo1OfRutasCollection1Rutas);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Nodo nodo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Nodo persistentNodo = em.find(Nodo.class, nodo.getId());
            Collection<Rutas> rutasCollectionOld = persistentNodo.getRutasCollection();
            Collection<Rutas> rutasCollectionNew = nodo.getRutasCollection();
            Collection<Rutas> rutasCollection1Old = persistentNodo.getRutasCollection1();
            Collection<Rutas> rutasCollection1New = nodo.getRutasCollection1();
            List<String> illegalOrphanMessages = null;
            for (Rutas rutasCollectionOldRutas : rutasCollectionOld) {
                if (!rutasCollectionNew.contains(rutasCollectionOldRutas)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rutas " + rutasCollectionOldRutas + " since its nodo field is not nullable.");
                }
            }
            for (Rutas rutasCollection1OldRutas : rutasCollection1Old) {
                if (!rutasCollection1New.contains(rutasCollection1OldRutas)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Rutas " + rutasCollection1OldRutas + " since its nodo1 field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Rutas> attachedRutasCollectionNew = new ArrayList<Rutas>();
            for (Rutas rutasCollectionNewRutasToAttach : rutasCollectionNew) {
                rutasCollectionNewRutasToAttach = em.getReference(rutasCollectionNewRutasToAttach.getClass(), rutasCollectionNewRutasToAttach.getRutasPK());
                attachedRutasCollectionNew.add(rutasCollectionNewRutasToAttach);
            }
            rutasCollectionNew = attachedRutasCollectionNew;
            nodo.setRutasCollection(rutasCollectionNew);
            Collection<Rutas> attachedRutasCollection1New = new ArrayList<Rutas>();
            for (Rutas rutasCollection1NewRutasToAttach : rutasCollection1New) {
                rutasCollection1NewRutasToAttach = em.getReference(rutasCollection1NewRutasToAttach.getClass(), rutasCollection1NewRutasToAttach.getRutasPK());
                attachedRutasCollection1New.add(rutasCollection1NewRutasToAttach);
            }
            rutasCollection1New = attachedRutasCollection1New;
            nodo.setRutasCollection1(rutasCollection1New);
            nodo = em.merge(nodo);
            for (Rutas rutasCollectionNewRutas : rutasCollectionNew) {
                if (!rutasCollectionOld.contains(rutasCollectionNewRutas)) {
                    Nodo oldNodoOfRutasCollectionNewRutas = rutasCollectionNewRutas.getHasta();
                    rutasCollectionNewRutas.setHasta(nodo);
                    rutasCollectionNewRutas = em.merge(rutasCollectionNewRutas);
                    if (oldNodoOfRutasCollectionNewRutas != null && !oldNodoOfRutasCollectionNewRutas.equals(nodo)) {
                        oldNodoOfRutasCollectionNewRutas.getRutasCollection().remove(rutasCollectionNewRutas);
                        oldNodoOfRutasCollectionNewRutas = em.merge(oldNodoOfRutasCollectionNewRutas);
                    }
                }
            }
            for (Rutas rutasCollection1NewRutas : rutasCollection1New) {
                if (!rutasCollection1Old.contains(rutasCollection1NewRutas)) {
                    Nodo oldNodo1OfRutasCollection1NewRutas = rutasCollection1NewRutas.getDesde();
                    rutasCollection1NewRutas.setDesde(nodo);
                    rutasCollection1NewRutas = em.merge(rutasCollection1NewRutas);
                    if (oldNodo1OfRutasCollection1NewRutas != null && !oldNodo1OfRutasCollection1NewRutas.equals(nodo)) {
                        oldNodo1OfRutasCollection1NewRutas.getRutasCollection1().remove(rutasCollection1NewRutas);
                        oldNodo1OfRutasCollection1NewRutas = em.merge(oldNodo1OfRutasCollection1NewRutas);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = nodo.getId();
                if (findNodo(id) == null) {
                    throw new NonexistentEntityException("The nodo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Nodo nodo;
            try {
                nodo = em.getReference(Nodo.class, id);
                nodo.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The nodo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Rutas> rutasCollectionOrphanCheck = nodo.getRutasCollection();
            for (Rutas rutasCollectionOrphanCheckRutas : rutasCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Nodo (" + nodo + ") cannot be destroyed since the Rutas " + rutasCollectionOrphanCheckRutas + " in its rutasCollection field has a non-nullable nodo field.");
            }
            Collection<Rutas> rutasCollection1OrphanCheck = nodo.getRutasCollection1();
            for (Rutas rutasCollection1OrphanCheckRutas : rutasCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Nodo (" + nodo + ") cannot be destroyed since the Rutas " + rutasCollection1OrphanCheckRutas + " in its rutasCollection1 field has a non-nullable nodo1 field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(nodo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Nodo> findNodoEntities() {
        return findNodoEntities(true, -1, -1);
    }

    public List<Nodo> findNodoEntities(int maxResults, int firstResult) {
        return findNodoEntities(false, maxResults, firstResult);
    }

    private List<Nodo> findNodoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Nodo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    
    public Nodo findNodo(String nombre){
        EntityManager em = getEntityManager();
        try{
            Nodo nodo = null;
            EntityTransaction entr = em.getTransaction();
            entr.begin();
            Query query = em.createNamedQuery("Nodo.findByNombre");
            //query.setParameter(1, nodo.getId());
            query.setParameter("nombre", nombre);
            //List list = query.getResultList();
            nodo = (Nodo)query.getSingleResult();
            //Iterator it = list.iterator();
            
            return nodo;
        }
        catch(Exception e){
            return null;
        }
        finally {
            em.close();
        }    
        
    }

    public Nodo findNodo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Nodo.class, id);
        } finally {
            em.close();
        }
    }

    public int getNodoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Nodo> rt = cq.from(Nodo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
